﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel.Design;
using ToolHelper.DataGridViewExtern;

namespace CommonLibrary.CustControls
{
    public partial class FrmEditCols : Form
    {
        private Int32 LstSltIndex;
        private ListBox LstCols;
        private DataGridView _grdView;
        private List<DataGridViewColumn> LstGrdCol;

        public FrmEditCols(DataGridView grdView)
        {
            InitializeComponent();
            DesignSurface surface = new DesignSurface();
            this.LstCols = new ListBox();
            this.LstCols.Dock = DockStyle.Fill;
            this.LstCols.ItemHeight = 18;
            this.LstCols.DrawMode = DrawMode.OwnerDrawFixed;
            this._grdView = grdView;
            this.InitializeColumns();
            this.LstCols.DrawItem += new DrawItemEventHandler(LstCols_DrawItem);
            this.LstCols.SelectedIndexChanged += new EventHandler(LstCols_SelectedIndexChanged);
            this.panel6.Controls.Add(this.LstCols);
        }

        private void BtnAddCol_Click(object sender, EventArgs e)
        {
            FrmAddCols addCols = new FrmAddCols(this);
            addCols.ShowDialog();
            addCols.Dispose();
        }

        public void CreateColumns(String colName, String colTitle, String colType, Boolean fillable, String gather)
        {
            CusToolboxItem colItem = new CusToolboxItem(colName, colTitle, colType, fillable);
            this.LstCols.Items.Add(colItem);
            CusToolboxItem.ControlNames.Add(colName);
            ExternPropTextBoxColumn grdCol = Activator.CreateInstance(typeof(ExternPropTextBoxColumn)) as ExternPropTextBoxColumn;
            grdCol.DisplayIndex = LstGrdCol.Count == 0 ? 0 : LstGrdCol.Count + 1;
            grdCol.Name = colName;
            grdCol.HeaderText = colTitle;
            grdCol.Fillable = fillable;
            grdCol.Gather = gather;
            this.LstGrdCol.Add(grdCol);
            this.ValidateDelButton();
        }

        private void BtnDelCol_Click(object sender, EventArgs e)
        {
            if (this.LstCols.Items[LstSltIndex] != null)
            {
                this.LstCols.Items.RemoveAt(LstSltIndex);
                this.LstGrdCol.RemoveAt(LstSltIndex);
                LstSltIndex--;
            }
            this.ValidateDelButton();
        }

        private void LstCols_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.LstCols.SelectedIndex == -1)
                return;
            LstSltIndex = this.LstCols.SelectedIndex;
            this.propertyGrid1.SelectedObject = this.LstCols.Items[LstSltIndex];
        }

        private void BtnUp_Click(object sender, EventArgs e)
        {
            if (LstSltIndex > 0 && LstSltIndex <= this.LstCols.Items.Count - 1)
            {
                CusToolboxItem item = this.LstCols.Items[LstSltIndex] as CusToolboxItem;
                this.LstCols.Items[LstSltIndex] = this.LstCols.Items[LstSltIndex - 1];
                this.LstCols.Items[LstSltIndex - 1] = item;
                ExternPropTextBoxColumn column = this.LstGrdCol[LstSltIndex] as ExternPropTextBoxColumn;
                this.LstGrdCol[LstSltIndex] = this.LstGrdCol[LstSltIndex - 1];
                this.LstGrdCol[LstSltIndex - 1] = column;
                this.LstCols.SelectedIndex = LstSltIndex - 1;
                this.LstCols.Refresh();
            }
        }

        private void BtnDowm_Click(object sender, EventArgs e)
        {
            if (LstSltIndex < this.LstCols.Items.Count - 1)
            {
                CusToolboxItem item = this.LstCols.Items[LstSltIndex] as CusToolboxItem;
                this.LstCols.Items[LstSltIndex] = this.LstCols.Items[LstSltIndex + 1];
                this.LstCols.Items[LstSltIndex + 1] = item;
                ExternPropTextBoxColumn column = this.LstGrdCol[LstSltIndex] as ExternPropTextBoxColumn;
                this.LstGrdCol[LstSltIndex] = this.LstGrdCol[LstSltIndex + 1];
                this.LstGrdCol[LstSltIndex + 1] = column;
                this.LstCols.SelectedIndex = LstSltIndex + 1;
                this.LstCols.Refresh();
            }
        }

        private void BtnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public DataGridViewColumnCollection GetColumns()
        {
            //var column = (ExternPropTextBoxColumn)_grdView.Columns[0];
            return this._grdView.Columns;
        }

        // 验证列名是否存在
        public Boolean ValidateColName(String colName)
        {
            for (int i = 0; i < this.LstCols.Items.Count; i++)
            {
                if (((CusToolboxItem)(this.LstCols.Items[i])).ControlName == colName)
                    return false;
            }
            return true;
        }

        // 验证删除按钮
        private void ValidateDelButton()
        {
            if (this.LstCols.Items.Count > 0)
                this.BtnDelCol.Enabled = true;
            else
                this.BtnDelCol.Enabled = false;
        }

        private void InitializeColumns()
        {
            Int32 colsNum = this._grdView.Columns.Count;
            this.LstGrdCol = new List<DataGridViewColumn>();
            for (int i = 0; i < colsNum; i++)
            {
                Type type = this._grdView.Columns[i].CellType;
                var col = this._grdView.Columns[i] as ExternPropTextBoxColumn;
                this.CreateColumns(this._grdView.Columns[i].Name, col.HeaderText, type.FullName.Replace("Cell", "Column"), col.Fillable, col.Gather);
            }
            this.ValidateDelButton();
        }

        private void LstCols_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
                return;
            Bitmap bitmap = null;
            CusToolboxItem item = LstCols.Items[e.Index] as CusToolboxItem;
            if (item != null)
            {
                Boolean isSelected = false;
                Graphics graphics = e.Graphics;
                bitmap = (Bitmap)item.ControlImg;
                if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    isSelected = true;
                if (isSelected)
                {
                    Rectangle r = e.Bounds;
                    r.Width--; r.Height--;
                    graphics.DrawRectangle(SystemPens.ActiveCaption, r);
                }
                else
                {
                    graphics.FillRectangle(SystemBrushes.Menu, e.Bounds);
                    using (Brush border = new SolidBrush(Color.FromArgb(Math.Min(SystemColors.Menu.R + 15, 255), Math.Min(SystemColors.Menu.G + 15, 255), Math.Min(SystemColors.Menu.B + 15, 255))))
                        graphics.FillRectangle(border, new Rectangle(e.Bounds.X, e.Bounds.Y, 20, e.Bounds.Height));
                }
                if (bitmap != null)
                    graphics.DrawImage(bitmap, e.Bounds.X + 2, e.Bounds.Y + 2, bitmap.Width, bitmap.Height);

                Brush textBrush = SystemBrushes.FromSystemColor(SystemColors.MenuText);
                graphics.DrawString(item.ControlName,
                    Font, textBrush,
                    e.Bounds.X + bitmap.Width + 2, e.Bounds.Y + 4);
            }
        }

        private void BtnConfirm_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            var cols = LstGrdCol.ToArray();
            var colCount = LstGrdCol.Count;
            for (int colIndex = 0; colIndex < colCount; colIndex++)
            {
                var toolItem = LstCols.Items[colIndex] as CusToolboxItem;
                cols[colIndex].Name = toolItem.ControlName;
                cols[colIndex].HeaderText = toolItem.ControlTitle;
                ((ExternPropTextBoxColumn)cols[colIndex]).Fillable = toolItem.Fillable == BoolEnum.True ? true : false;
                //cols[colIndex].Fillable = toolItem.Fillable == BoolEnum.True ? true : false;
            }
            this._grdView.Columns.Clear();
            this._grdView.Columns.AddRange(LstGrdCol.ToArray());
            this.Close();
        }
    }
}
